package org.tim.text;

import java.util.Date;

public class RelativeTimeFormat {
	
	// TODO: Implement abbreviation
	private boolean abriv = false;
	
	/**
	 * Format the time between a begin and end time to a simple string eg. 5 days, 12 minutes or 5 hours
	 * @param b begin time that represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
	 * @param e end time represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
	 * @return the formatted string
	 */
	public static String format ( long b , long e ) {
		long d = ( e - b );
		if ( d <= 1000 ) {
			return doFormat( d , "millisecond" );
		}
		if ( (d/1000) <= 60 ) {
			return doFormat( (d/1000) , "second" );
		}
		if ( (d/1000/60) <= 60 ) {
			return doFormat( (d/1000/60) , "minute" );
		}
		if ( (d/1000/60/60) <= 24 ) {
			return doFormat( (d/1000/60/60) , "hour" );
		}
		
		return doFormat((d/1000/60/60/24), "day" );
	}

	/**
	 * Format the time between a begintime and the currenttime to a simple string eg. 5 days, 12 minutes or 5 hours
	 * @param b begin time that represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.
	 * @return the formatted string
	 */	
	public static String format ( long b ) {
		return format ( b , System.currentTimeMillis() );
	}

	/**
	 * Format the time between a begintime and the currenttime to a simple string eg. 5 days, 12 minutes or 5 hours
	 * @param b Date object of begin time
	 * @return the formatted string
	 */	
	public static String format ( Date b ) {
		return format ( b.getTime() , System.currentTimeMillis() );
	}
	
	/**
	 * Format the time between a begin and end time to a simple string eg. 5 days, 12 minutes or 5 hours
	 * @param b Date object of begin time
	 * @param e Date object of end time
	 * @return the formatted string
	 */
	public static String format ( Date b , Date e ) {
		return format(b.getTime(),e.getTime());
	}
	
	/**
	 * Internal function to paste concat values
	 * @param val the long value
	 * @param postfix the text to past behind the value
	 * @return concatenated string
	 */
	protected static String doFormat ( long val , String postfix ) {
		return "" + val + " " + ( val > 1 ? postfix + "s" : postfix );
	}

}